using AjaxControlToolkit;
using ASP;
using ClassLibrary.BusinessLayer.Facade;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HeadingSet : Page, IRequiresSessionState
{
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setButtons(string.Empty);
        this.setTextReadOnly(true);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProcedureSet");
        if (!canDelete.CanDelete())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoDelete.ToString();
        }
        else
        {
            List<string> lstValues;
            try
            {
                ProcedureSetFacade facade = new ProcedureSetFacade();
                SqlParameter[] sqlparam = new SqlParameter[2];
                sqlparam[0] = new SqlParameter();
                sqlparam[0].ParameterName = "@HeadingSet";
                sqlparam[0].Value = this.txtHeadingSet.Text.ToString();
                sqlparam[1] = new SqlParameter();
                sqlparam[1].ParameterName = "@Infobar";
                sqlparam[1].Value = "";
                sqlparam[1].DbType = DbType.String;
                sqlparam[1].Size = 100;
                sqlparam[1].Direction = ParameterDirection.InputOutput;
                DbParameter[] param = sqlparam;
                int intRet = facade.DeleteProcedureSet("_IPMS_HeadingSetDeleteSp", param);
                lstValues = new List<string>();
                this.lblError.Text = IPDataFormat.GenerateMessage(sqlparam[1].Value.ToString(), lstValues);
                this.lblError.Visible = true;
                if (this.lblError.Text == "")
                {
                    this.clearText();
                    this.Search();
                    this.setButtons(string.Empty);
                    this.setTextReadOnly(true);
                }
            }
            catch (Exception ex)
            {
                lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.lblError.Visible = true;
            }
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        this.setButtons("modify");
        this.setTextReadOnly(false);
        this.txtHeadingSet.Enabled = false;
        this.btnCountry.Enabled = false;
        this.txtHeadingSetName.Focus();
        this.txtHeadingSetName.Attributes.Add("onfocusin", " select();");
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setButtons("new");
        this.setTextReadOnly(false);
        this.txtHeadingSet.Focus();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.cbForUpdate.Checked)
        {
            this.ODSHeadingSet.UpdateMethod = "Update";
            this.ODSHeadingSet.UpdateParameters["HeadingSetName"].DefaultValue = this.txtHeadingSetName.Text.ToString();
            this.ODSHeadingSet.UpdateParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
            this.ODSHeadingSet.UpdateParameters["HeadingSet"].DefaultValue = this.txtHeadingSet.Text.ToString();
            this.ODSHeadingSet.Update();
        }
        else
        {
            try
            {
                this.ODSHeadingSet.InsertMethod = "Insert";
                this.ODSHeadingSet.InsertParameters["HeadingSet"].DefaultValue = this.txtHeadingSet.Text.ToString();
                this.ODSHeadingSet.InsertParameters["HeadingSetName"].DefaultValue = this.txtHeadingSetName.Text.ToString();
                this.ODSHeadingSet.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSHeadingSet.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSHeadingSet.InsertParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSHeadingSet.Insert();
                this.setButtons(string.Empty);
                this.setTextReadOnly(true);
                this.cbForUpdate.Checked = false;
                this.clearText();
                this.Search();
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.lblError.Visible = true;
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.Search();
    }

    protected void btnSetBlank_Click(object sender, EventArgs e)
    {
        this.txtHeadingSet.Text = string.Empty;
        this.setButtons("new");
        this.setTextReadOnly(false);
        this.cbForUpdate.Checked = false;
    }

    protected void clearText()
    {
        this.txtHeadingSet.Text = string.Empty;
        this.txtHeadingSetName.Text = string.Empty;
        this.cbForUpdate.Checked = false;
        this.lblError.Text = "";
        this.lblError.Visible = false;
    }

    protected void gvChooseCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void gvHS_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvHS.PageIndex = e.NewPageIndex;
        this.gvHS.DataBind();
        this.Search();
    }

    protected void gvHS_RowCreated(object sender, GridViewRowEventArgs e)
    {
        this.Sort(e, this.gvHS);
    }

    protected void gvHS_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.clearText();
        this.chkForOperation.Checked = true;
        this.cbForUpdate.Checked = true;
        this.RefreshProcedureSetTemplate();
        this.setButtons(string.Empty);
    }

    protected void gvHST_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvHST.PageIndex = e.NewPageIndex;
        this.RefreshProcedureSetTemplate();
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void lbtnProcedureSet_Click(object sender, EventArgs e)
    {
        string strHeadingSet = base.Server.UrlEncode(this.txtHeadingSet.Text.ToString());
        base.Response.Redirect("HeadingSetTmplt.aspx?HeadingSet=" + strHeadingSet);
    }

    protected void OKButton_Click(object sender, EventArgs e)
    {
        if (this.gvChooseCountry.SelectedIndex >= 0)
        {
            this.txtHeadingSet.Text = this.gvChooseCountry.DataKeys[this.gvChooseCountry.SelectedIndex].Values["Country"].ToString();
            this.setButtons("new");
            this.setTextReadOnly(false);
            this.cbForUpdate.Checked = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProcedureSet");
        if (!canView.CanView())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else
        {
            this.setButtons(string.Empty);
            this.setTextReadOnly(true);
            if (!this.Page.IsPostBack)
            {
                ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.ProcedureSetup + " | " + ISitemap.ProcedureSet; ;

                this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
            }
        }
    }

    protected void RefreshProcedureSetTemplate()
    {
        try
        {
            HeadingSets runMethod = new HeadingSets();
            runMethod.GetHeadingSetInfo(this.gvHS.DataKeys[this.gvHS.SelectedIndex].Values["HeadingSet"].ToString());
            this.txtHeadingSet.Text = this.gvHS.DataKeys[this.gvHS.SelectedIndex].Values["HeadingSet"].ToString();
            this.txtHeadingSetName.Text = runMethod.HeadingSetName;
            ProcedureSetTmpltFacade facade = new ProcedureSetTmpltFacade();
            SqlParameter[] sqlparam = new SqlParameter[2];
            sqlparam[0] = new SqlParameter();
            sqlparam[0].ParameterName = "@HeadingSet";
            sqlparam[0].Value = this.txtHeadingSet.Text.ToString();
            sqlparam[1] = new SqlParameter();
            sqlparam[1].ParameterName = "@HeadingCode";
            sqlparam[1].Value = "";
            DbParameter[] param = sqlparam;
            DataView dv = facade.GetsProcedureSetTmplt("_IPMS_HeadingSetTemplateSearch_SP", param).DefaultView;
            this.gvHST.DataSourceID = string.Empty;
            this.gvHST.DataSource = dv;
            this.gvHST.DataBind();
        }
        catch (NullReferenceException)
        {
        }
    }

    protected void Search()
    {
        ClientsFacade facade = new ClientsFacade();
        SqlParameter[] sqlparam = new SqlParameter[2];
        sqlparam[0] = new SqlParameter();
        sqlparam[0].ParameterName = "@HeadingSet";
        sqlparam[0].Value = this.txtSearchProcSet.Text.ToString();
        sqlparam[1] = new SqlParameter();
        sqlparam[1].ParameterName = "@HeadingSetName";
        sqlparam[1].Value = this.txtSearchProcSetName.Text.ToString();
        DbParameter[] param = sqlparam;
        DataView dv = facade.GetsClients("_IPMS_HeadingSetSearch_SP", param).DefaultView;
        this.gvHS.DataSourceID = string.Empty;
        this.gvHS.DataSource = dv;
        this.gvHS.DataBind();
    }

    protected void setButtons(string action)
    {
        switch (action)
        {
            case "new":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;

            case "modify":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;
        }
        this.btnNew.Enabled = true;
        this.btnModify.Enabled = this.txtHeadingSet.Text.Length != 0;
        this.btnSave.Enabled = false;
        this.btnDelete.Enabled = this.txtHeadingSet.Text.Length != 0;
    }

    protected void setTextReadOnly(bool bBool)
    {
        this.txtHeadingSet.ReadOnly = bBool;
        this.txtHeadingSetName.ReadOnly = bBool;
    }

    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";
                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }
}
